package com.yc.boot.sys.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.boot.common.model.response.Outcome;
import com.yc.boot.sys.entity.SysUser;
import com.yc.boot.sys.business.SysUserBusiness;
import com.yc.boot.sys.model.UserInfo;
import com.yc.boot.sys.model.UserLog;
import com.yc.boot.sys.model.find.FindLogPage;
import com.yc.boot.sys.model.find.FindUserPage;
import com.yc.boot.sys.model.save.RegisterUser;
import com.yc.boot.sys.model.save.SaveUser;
import com.yc.boot.sys.model.update.UpdatePassword;
import com.yc.boot.sys.model.update.UpdateUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.List;

import static com.yc.boot.common.constant.BaseConstant.ONE;

/**
 * @author 杨智杰
 * @since 2021/8/13 8:42
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/sys/user")
public class SysUserApi {

    @Resource
    private SysUserBusiness sysUserBusiness;

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/getLoginUser")
    public Outcome<UserInfo> getLoginUser() {
        UserInfo userInfo = sysUserBusiness.getUserInfo();
        return ObjectUtil.isNotNull(userInfo)
                ? Outcome.success(userInfo)
                : Outcome.failure("未登录!").setCode(401);
    }


    @GetMapping("/{id}")
    @ApiOperation("详情")
    public Outcome<UserInfo> info(@PathVariable Long id) {
        UserInfo userInfo = sysUserBusiness.getUserInfo(id);
        return ObjectUtil.isNotNull(userInfo)
                ? Outcome.success(userInfo)
                : Outcome.failure("未查询到相关数据!");
    }

    @GetMapping
    @ApiOperation("分页")
    public Outcome<IPage<SysUser>> page(FindUserPage param) {
        return Outcome.success(sysUserBusiness.page(param));
    }

    @PostMapping
    @ApiOperation("新增")
    public Outcome save(@RequestBody @Valid SaveUser param) {
        return sysUserBusiness.save(param);
    }

    @PostMapping("/register")
    @ApiOperation("注册")
    public Outcome register(@RequestBody @Valid RegisterUser param) {
        return sysUserBusiness.save(Convert.convert(SaveUser.class, param).setStatus(ONE));
    }

    @PutMapping
    @ApiOperation("修改")
    public Outcome update(@RequestBody @Valid UpdateUser param) {
        return sysUserBusiness.update(param);
    }

    @PutMapping("/image")
    @ApiOperation("修改")
    public Outcome updateImage(@RequestBody @Valid UpdateUser param) {
        return sysUserBusiness.updateIMage(param);
    }

    @DeleteMapping
    @ApiOperation("删除")
    public Outcome delete(@RequestBody @Valid @Size(min = 1) List<Long> id) {
        return Outcome.status(sysUserBusiness.delete(id));
    }

    @GetMapping("/restPassword")
    @ApiOperation("重置密码")
    public Outcome restPassword(@RequestParam("id") Long id) {
        return Outcome.status(sysUserBusiness.restPassword(id));
    }

    @PostMapping("/updatePassword")
    @ApiOperation("修改密码")
    public Outcome updatePassword(@RequestBody @Valid UpdatePassword param) {
        return sysUserBusiness.updatePassword(param);
    }

    @GetMapping("/log")
    @ApiOperation("日志分页")
    public Outcome<IPage<UserLog>> logPage(FindLogPage param) {
        return Outcome.success(sysUserBusiness.logPage(param));
    }
}
